﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BUS;
using DTO;
using System.IO;
namespace QUANLYDIEMHOCSINH
{
    public partial class THONGKE : Form
    {
        HOCKYBUS hkbus = new HOCKYBUS();
        LOPBUS lopbus = new LOPBUS();
        NAMHOCBUS nambus = new NAMHOCBUS();
        KHOIBUS khoibus = new KHOIBUS();
        MONHOCBUS monbus = new MONHOCBUS();
        public THONGKE()
        {
            InitializeComponent();
            set_AN();
            LoadDanhSachHocKy();
            LoadDanhSachNamHoc();
            LoadDanhSachMon();
            if(rdLop.Checked==true)
            {
                LoadDanhSachLop();
            }
            else
            {
                LoadDanhSachKhoi();
            }
        }
        void set_AN()
        {
            if (File.Exists("AnhNen.txt"))
            {
                FileStream fs = new FileStream("AnhNen.txt", FileMode.Open);
                StreamReader sr = new StreamReader(fs);
                string s = null;
                string t = null;
                while ((s = sr.ReadLine()) != null)
                {
                    t = s;
                }
                this.BackColor = Color.FromName(t);
                fs.Close();
            }
            else
            {
                FileStream fs = new FileStream("AnhNen.txt", FileMode.Append);
                StreamWriter sw = new StreamWriter(fs);
                sw.WriteLine("Turquoise");
                sw.Flush();
                sw.Close();
                fs.Close();

                FileStream fs1 = new FileStream("AnhNen.txt", FileMode.Open);
                StreamReader sr1 = new StreamReader(fs1);
                string s = null;
                string t = null;
                while ((s = sr1.ReadLine()) != null)
                {
                    t = s;
                }
                this.BackColor = Color.FromName(t);
                fs1.Close();
            }
        }
        void LoadDanhSachMon()
        {
            List<MONHOCDTO> dt = monbus.LoadDanhSachMon();
            MONHOCDTO m = new MONHOCDTO();
            m.MAMH = 100;
            m.TENMH ="Tất Cả";
            dt.Add(m);
            cbMon.DataSource = dt;
            cbMon.DisplayMember = "TENMH";
            cbMon.ValueMember = "MAMH";
            cbMon.SelectedIndex = 0;
        }

        void LoadDanhSachHocKy()
        {
            cbHocKy.DataSource = hkbus.LoadDachHocKy();
            cbHocKy.DisplayMember = "TENHK";
            cbHocKy.ValueMember = "MAHK";
            cbHocKy.SelectedIndex = 0;
        }

        void LoadDanhSachLop()
        {
            cbDoiTuong.DataSource = lopbus.layDSL();
            cbDoiTuong.DisplayMember = "TENLOP";
            cbDoiTuong.ValueMember = "MALOP";
            cbDoiTuong.SelectedItem = 0;
        }

        void LoadDanhSachKhoi()
        {
            cbDoiTuong.DataSource = khoibus.DanhSachKhoi();
            cbDoiTuong.DisplayMember = "TENKH";
            cbDoiTuong.ValueMember = "MAKH";
            cbDoiTuong.SelectedItem = 0;
        }

        void LoadDanhSachNamHoc()
        {
            cbNam.DataSource = nambus.LoadDanhSachNamHoc();
            cbNam.DisplayMember = "NAMBATDAU";
            cbNam.ValueMember = "MANAMHOC";
        }

        private void rdKhoi_CheckedChanged(object sender, EventArgs e)
        {
            if (rdLop.Checked == true)
            {
                LoadDanhSachLop();
            }
            else
            {
                LoadDanhSachKhoi();
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (rdLop.Checked == true)
            {
                if (cbMon.SelectedValue.ToString() != "100")
                {
                    frmThongKeTheoLop tkl = new frmThongKeTheoLop();
                    tkl.MaNam = int.Parse(cbNam.SelectedValue.ToString());
                    tkl.MaMon = int.Parse(cbMon.SelectedValue.ToString());
                    tkl.MaLop = int.Parse(cbDoiTuong.SelectedValue.ToString());
                    tkl.NamHoc = cbNam.Text;
                    tkl.Mon = cbMon.Text;
                    tkl.HocKy = cbHocKy.Text;
                    tkl.MaHocKy = int.Parse(cbHocKy.SelectedValue.ToString());
                    tkl.ShowDialog();
                }
                else
                {
                    frmThongKeTatCaMonTheoLop tk = new frmThongKeTatCaMonTheoLop();
                    tk.MaLop = int.Parse(cbDoiTuong.SelectedValue.ToString());
                    tk.TenLop = cbDoiTuong.Text;
                    tk.MaNam = int.Parse(cbNam.SelectedValue.ToString());
                    tk.NamHoc = cbNam.Text;
                    tk.MaHocKy = int.Parse(cbHocKy.SelectedValue.ToString());
                    tk.HocKy = cbHocKy.Text;
                    tk.ShowDialog();
                }
            }
            else
            {
                if (cbMon.SelectedValue.ToString() != "100")
                {
                    frmThongKeTheoKhoi tkk = new frmThongKeTheoKhoi();
                    tkk.MaNam = int.Parse(cbNam.SelectedValue.ToString());
                    tkk.MaMon = int.Parse(cbMon.SelectedValue.ToString());
                    tkk.MaKhoi = int.Parse(cbDoiTuong.SelectedValue.ToString());
                    tkk.NamHoc = cbNam.Text;
                    tkk.Mon = cbMon.Text;
                    tkk.HocKy = cbHocKy.Text;
                    tkk.MaHocKy = int.Parse(cbHocKy.SelectedValue.ToString());
                    tkk.ShowDialog();
                }
                else
                {
                    frmThongKeTatCaMonTheoKhoi tkk = new frmThongKeTatCaMonTheoKhoi();
                    tkk.MaNam = int.Parse(cbNam.SelectedValue.ToString());
                    tkk.MaKhoi = int.Parse(cbDoiTuong.SelectedValue.ToString());
                    tkk.NamHoc = cbNam.Text;
                    tkk.HocKy = cbHocKy.Text;
                    tkk.MaHocKy = int.Parse(cbHocKy.SelectedValue.ToString());
                    tkk.ShowDialog();
                }
            }
        }
    }
}
